package com.yjhb.android.sbz.adapter;

import android.content.Context;
import android.graphics.Typeface;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.entity.RedPackDetails;
import com.yjhb.android.sbz.entity.TaskInfo;
import com.yjhb.android.sbz.mvp.sendredpack.SendRedPackPresenter;
import com.yjhb.android.sbz.mvp.sendredpack.SendRedPackView;
import com.yjhb.android.sbz.tool.LoadingDialogTool;
import com.yjhb.android.sbz.tool.Tool;
import com.jojo.android.zxlib.base.BaseRecyclerAdapter;
import com.jojo.android.zxlib.tool.ZxSpanTool;
import com.jojo.android.zxlib.view.dialog.ILoading;

import java.math.BigDecimal;

/**
 * 创建者 zx
 * 创建时间 2020/11/18 17:31
 */
public class TaskListItemAdapter extends BaseRecyclerAdapter<TaskListItemAdapter.Holder, TaskInfo> {
    private final String TAG = "TaskListItemAdapter";


    public TaskListItemAdapter(Context context) {
        super(context);
    }

    @Override
    protected Holder getHolder(View v) {
        return new Holder(v);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.item_list_task;
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int i) {
        final TaskInfo task = data.get(i);
        Glide.with(context).load(task.getAvatar()).apply(new RequestOptions().placeholder(R.mipmap.test_head)).into(holder.head_iv);
        holder.title_tv.setText(task.getTitle());
        holder.people_num_tv.setText(task.getSuccess() + "人");
        holder.num_tv.setText(task.getCurrentStock() + "个");
        BigDecimal bigDecimal = Tool.getPrettyNumber(task.getMoney());
        CharSequence money = "+" + bigDecimal + "元";
        money = ZxSpanTool.ZoomSpan(money, money.length() - 1, money.length(), 0.7f);
        money = ZxSpanTool.ZoomSpan(money, 0, 1, 0.7f);
        money = ZxSpanTool.StyleSpan(money, 1, money.length() - 1, Typeface.BOLD);
        holder.money_tv.setText(money);
        holder.label1.setText(Tool.idTransformString(task.getCategoryId()));
        holder.label2.setText(task.getName());

        int vipLevel = task.getVipLevel();
        switch (vipLevel) {
            case 100:
                holder.vip_iv.setImageResource(R.mipmap.exp);
                holder.vip_iv.setVisibility(View.VISIBLE);
                break;
            case 1:
                holder.vip_iv.setImageResource(R.mipmap.month);
                holder.vip_iv.setVisibility(View.VISIBLE);
                break;
            case 2:
                holder.vip_iv.setImageResource(R.mipmap.season);
                holder.vip_iv.setVisibility(View.VISIBLE);
                break;
            case 3:
                holder.vip_iv.setImageResource(R.mipmap.year);
                holder.vip_iv.setVisibility(View.VISIBLE);
                break;
            case 4:
                holder.vip_iv.setImageResource(R.mipmap.half_year);
                holder.vip_iv.setVisibility(View.VISIBLE);
                break;
            default:
                holder.vip_iv.setVisibility(View.GONE);
                break;
        }
        if (task.getCreditMoney() != null && task.getCreditMoney().intValue() >= 100) {
            holder.bao_tv.setVisibility(View.VISIBLE);
        } else {
            holder.bao_tv.setVisibility(View.GONE);
        }
        if (task.isRecommend()) {
            holder.ding_tv.setVisibility(View.VISIBLE);
        } else {
            holder.ding_tv.setVisibility(View.GONE);
        }
        if (task.getAuditFast() == 1) {
            holder.fast_iv.setVisibility(View.VISIBLE);
        } else {
            holder.fast_iv.setVisibility(View.GONE);
        }
        if (task.getRedPacket() == 1) {
            holder.red_envelop.setVisibility(View.VISIBLE);
            holder.red_envelop.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (Tool.checkLogin(context)){
                        ILoading loading = LoadingDialogTool.createDialog(context);
                        SendRedPackPresenter presenter = new SendRedPackPresenter(new SendRedPackView() {
                            @Override
                            public void onDetail(RedPackDetails details) {
                                loading.cancel();
                                Tool.startRedDetail(context, details);
                            }

                            @Override
                            public void onWait() {
                                loading.setTipsText("正在加载...");
                                loading.show();
                            }

                            @Override
                            public void onSuccess(Object data) {

                            }

                            @Override
                            public void onFailure(int code, String msg) {

                            }
                        });
                        presenter.queryRedPacket(task.getId());
                    }
                }
            });
        } else {
            holder.red_envelop.setVisibility(View.GONE);
        }

        final int i1 = i;
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemClickListener != null) {
                    itemClickListener.onItemClick(i1, data.get(i1));
                }
            }
        });
    }


    class Holder extends RecyclerView.ViewHolder {
        ImageView head_iv;
        TextView title_tv;
        TextView people_num_tv;
        TextView num_tv;
        TextView money_tv;
        TextView label1;
        TextView label2;
        View bao_tv;
        View ding_tv;
        View fast_iv;
        ImageView vip_iv;
        ImageView red_envelop;

        public Holder(@NonNull View itemView) {
            super(itemView);
            head_iv = itemView.findViewById(R.id.head_iv);
            title_tv = itemView.findViewById(R.id.title_tv);
            people_num_tv = itemView.findViewById(R.id.people_num_tv);
            num_tv = itemView.findViewById(R.id.num_tv);
            money_tv = itemView.findViewById(R.id.money_tv);
            label1 = itemView.findViewById(R.id.label1);
            label2 = itemView.findViewById(R.id.label2);
            bao_tv = itemView.findViewById(R.id.bao_tv);
            ding_tv = itemView.findViewById(R.id.ding_tv);
            fast_iv = itemView.findViewById(R.id.fast_iv);
            vip_iv = itemView.findViewById(R.id.vip_iv);
            red_envelop = itemView.findViewById(R.id.red_envelop);
        }
    }

}
